package com.iconsys.interceptor;

import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class CharsetInterceptor extends HandlerInterceptorAdapter {
  private String charset = null;

  public void setCharset(String charset) {
    this.charset = charset;
  }

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    String className = handler.getClass().getName(); // package Name .ClassName
    System.out.println(className);
    request.setCharacterEncoding("UTF-8");
    return true;
  }

  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    if (modelAndView != null) {
      Set set = modelAndView.getModel().entrySet();
      Iterator it = set.iterator();
      System.out.println("--------------request paramter---------------");
      while (it.hasNext()) {
        Entry<String, Object> entry = (Entry<String, Object>) it.next();
        System.out.println(entry.getKey() + "  " + entry.getValue());
      }
      System.out.println("---------------------------------------------");
      System.out.println(modelAndView.getViewName());
      modelAndView.addObject("res", "/iconsys/res");
    }
  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  }
}
